package controllers

import config._
import dao.{CmDao, CsDao, EpigDao, GenomeDao}
import javax.inject.Inject
import models.Tables.{CmRow, CsRow, EpigeneticsRow, GenomeRow}
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}

class InsertController @Inject()(csDao: CsDao,
                                 cmDao: CmDao,
                                 genomeDao: GenomeDao,
                                 epigDao: EpigDao,
                                 cc: ControllerComponents) extends AbstractController(cc) with MyFile with MyAwait {


  def insert(insert: String) = Action {
    insert match {
      case "insertCs" => insertCs
      case "insertCm" => insertCm
      case "insertGenome" => insertGenome
      case "insertEpig" => insertEpig
    }
    Ok(Json.toJson("success"))
  }


  def insertCs = {
    val gff = "D:\\黄瓜喝南瓜信息数据库开发\\黄瓜和南瓜信息数据库开发\\2021.5.12相关数据/Gy14_v2.gene.gff".readLines

    val row = gff.filter(!_.startsWith("#")).filter(_.split("\t")(2) == "gene").map { line =>
      val lines = line.split("\t")

      val last = lines.last.split(";")
      val geneId = last.head.drop(3)
      val note = last.last.drop(5)
      val chr = lines.head
      val start = lines(3)
      val end = lines(4)
      val strand = lines(6)
      CsRow(0, geneId, chr, start.toInt, end.toInt, strand, note)
    }.toSeq
    csDao.insertGene(row).toAwait
  }

  def insertCm = {
    val path = "D:\\黄瓜和南瓜信息数据库开发\\黄瓜和南瓜数据\\2021.5.24更新要求\\Cm"
    val gff = s"$path/Cmoschata_v1.gff3".readLines
    val anno = s"$path/Cm_anno.txt".readLines.tail.map { x =>
      val lines = (x.trim + " ").split("\t")
      val gene = lines.head.split('.').head
      gene.trim -> lines.tail
    }.toMap

    val row = gff.filter(!_.startsWith("#")).filter(_.split("\t")(2) == "gene").map { line =>
      val lines = line.split("\t")

      val last = lines.last.split(";")
      val geneId = last.head.drop(3)
      val an = anno(geneId)
      val des = an.head.replaceAll("\"", "")
      val interpro = (if (an.length > 1) an(1) else "").replaceAll("\"", "")
      val go = (if (an.length > 2) an.last else "").replaceAll("\"", "")

      val chr = lines.head
      val start = lines(3)
      val end = lines(4)
      val strand = lines(6)
      CmRow(0, geneId, chr, start.toInt, end.toInt, strand, des, interpro, go)
    }.toSeq
    cmDao.insertGene(row).toAwait
  }


  def insertGenome = {
    val path = "D:\\黄瓜和南瓜信息数据库开发\\数据库修改意见7-14\\数据库修改意见7-14"

    val cm = s"$path/cm.txt".readLines.map { x =>
      val line = x.split("\t")
      GenomeRow(0, line.head, line(1), line(2), line(3), line(4), line(5), line(6),line(7),line(8),"no")
    }

    val cs = s"$path/cs.txt".readLines.map { x =>
      val line = x.split("\t")
      GenomeRow(0, line.head, line(1), line(2), line(3), line(4), line(5), line(6),line(7),line(8),line(9))
    }

    genomeDao.insert((cm ++ cs).toSeq).toAwait

    Ok(Json.toJson("success"))

  }

  def insertEpig = {
    val path = "D:\\黄瓜和南瓜信息数据库开发\\methylation"

    path.listFile.foreach { x =>
      val name = x.getName.split('.').head.replace("CsPm","CsVm")
      val row = x.readLines.map { x =>
        val line = x.split("\t")
        EpigeneticsRow(0, line.head, name, line(1), line(2).replace("Phloem exudate","Vascular exudation"), line(3), line(4), line(5).toInt, line(6).toInt, line(7), line(8).replace("CsPm","CsVm"))
      }.toSeq
      epigDao.insert(row).toAwait
    }
    Ok(Json.toJson("success"))
  }

}
